import os
import logging
import logging.handlers
from flask import Flask

# 配置日志
def setup_logging(app:Flask):
    log_directory = app.config['CONFIG']['log_directory']
    log_file_name = 'app.log'
    log_file_path = os.path.join(log_directory, log_file_name)

    if not os.path.exists(log_directory):
        os.makedirs(log_directory)
    
    logger = logging.getLogger()
    handler = logging.handlers.TimedRotatingFileHandler(log_file_path, when="midnight", interval=1, backupCount=10,encoding='utf-8')
    formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
    handler.setFormatter(formatter)
    logger.addHandler(handler)
    logger.setLevel(logging.DEBUG)
    app.logger=logger